package com.zds.demo;
// MainActivity.java

import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = findViewById(R.id.view_pager);
        radioGroup = findViewById(R.id.radio_group);

        List<List<String>> tabData = new ArrayList<>();
        // 每页增加更多item，确保无法完全显示
        tabData.add(Arrays.asList("Item 1-1", "Item 1-2", "Item 1-3", "Item 1-4"));
        tabData.add(Arrays.asList("Item 2-1", "Item 2-2", "Item 2-3", "Item 2-4", "Item 2-5", "Item 2-6"));
        tabData.add(Arrays.asList("Item 3-1", "Item 3-2", "Item 3-3", "Item 3-4", "Item 3-5", "Item 3-6"));

        ViewPagerAdapter adapter = new ViewPagerAdapter(tabData);
        viewPager.setAdapter(adapter);

        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            int position = checkedId == R.id.radio1 ? 0 :
                    (checkedId == R.id.radio2 ? 1 : 2);
            viewPager.setCurrentItem(position);
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                ((RadioButton) radioGroup.getChildAt(position)).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    class ViewPagerAdapter extends androidx.viewpager.widget.PagerAdapter {
        private final List<List<String>> data;

        public ViewPagerAdapter(List<List<String>> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public boolean isViewFromObject(android.view.View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(android.view.ViewGroup container, int position) {
            // 创建外层容器，设置固定高度
            LinearLayout containerLayout = new LinearLayout(MainActivity.this);
            containerLayout.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    400 // 固定高度，确保无法显示全部item
            );
            containerLayout.setLayoutParams(params);

            // 创建ListView
            ListView listView = new ListView(MainActivity.this);
            listView.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT
            ));

            ArrayAdapter<String> adapter = new ArrayAdapter<>(
                    MainActivity.this,
                    android.R.layout.simple_list_item_1,
                    data.get(position)
            );
            listView.setAdapter(adapter);
            // 确保父容器不拦截事件
            listView.setOnTouchListener((v, event) -> {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            });
            // 将ListView添加到容器中
            containerLayout.addView(listView);
            container.addView(containerLayout);

            return containerLayout;

        }

        @Override
        public void destroyItem(android.view.ViewGroup container, int position, Object object) {
            container.removeView((android.view.View) object);
        }
    }
}